package com.example.back.mapper;

import java.util.List;
import java.util.Map;

import com.example.common.entity.RxDrugs;
import com.example.common.entity.RxInfo;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectProvider;

public interface RxMapper {
    @SelectProvider(type = SqlBuilder.class, method = "RxListSQL")
    List<Map<String, Object>> RxList(@Param("rxorderno") String rxorderno, @Param("orderno") String orderno,
            @Param("dName") String dName, @Param("pName") String pName, @Param("cName") String cName,
            @Param("DType") byte DType, @Param("ChkStatus") int ChkStatus, @Param("skip") int skip,
            @Param("limit") int limit);

    @SelectProvider(type = SqlBuilder.class, method = "RxListCountSQL")
    Integer RxListCount(@Param("rxorderno") String rxorderno, @Param("orderno") String orderno,
            @Param("dName") String dName, @Param("pName") String pName, @Param("cName") String cName,
            @Param("DType") byte DType, @Param("ChkStatus") int ChkStatus);

    @Select("select a.create_time_datetime as CreateTime,a.rx_order_no_nvarchar as RxOrderNO,a.order_no_nvarchar as OrderNO,"
    + " cast(a.d_type_tinyint as SIGNED INTEGER) as DType,c.name_nvarchar as Name,c.sex_nvarchar as Sex,c.age_int as Age,"
    + " e.name_nvarchar as depName,cast(a.chk_status_tinyint as SIGNED INTEGER) as ChkStatus,a.open_doctor_nvarchar as OpenDoctor,"
    + " a.chk_doctor_nvarchar as ChkDoctor,a.failure_reason_nvarchar as FailureReason,a.failure_remark_nvarchar as FailureRemark,"
    + " a.dialectical_nvarchar as Dialectical,a.chk_time_datetime as ChkTime"
    + " from order_rx_main a"
    + " inner join order_main b on a.order_no_nvarchar = b.order_no_nvarchar"
    + " inner join order_patients_info c on b.patients_info_id_bigint = c.id_bigint"
    + " inner join doctor_base_info d on b.dc_id_bigint = d.dc_id"
    + " inner join h_hospital_department e on d.depart_id = e.id_bigint"
    + " where a.rx_order_no_nvarchar = #{rxorderno}")
    Map<String, Object> RxOrderDetail(@Param("rxorderno") String rxorderno);

    


    class SqlBuilder {
        public String RxListSQL(@Param("rxorderno") String rxorderno, @Param("orderno") String orderno,
                @Param("dName") String dName, @Param("pName") String pName, @Param("cName") String cName,
                @Param("DType") byte DType, @Param("ChkStatus") int ChkStatus, @Param("skip") int skip,
                @Param("limit") int limit) {
            String sql = "with paget as (" + " select a.rx_order_no_nvarchar from order_rx_main a"
                    + " inner join order_main b on a.order_no_nvarchar = b.order_no_nvarchar"
                    + " inner join order_patients_info e on b.patients_info_id_bigint = e.id_bigint" + " where 1 = 1";
            if (!rxorderno.isEmpty() && rxorderno.length() > 0)
                sql += " and a.rx_order_no_nvarchar like CONCAT('%',#{rxorderno},'%')";
            if (!orderno.isEmpty() && orderno.length() > 0)
                sql += " and a.order_no_nvarchar like CONCAT('%',#{orderno},'%')";
            if (!dName.isEmpty() && dName.length() > 0)
                sql += " and a.open_doctor_nvarchar like CONCAT('%',#{dName},'%')";
            if (!pName.isEmpty() && pName.length() > 0)
                sql += " and a.chk_doctor_nvarchar like CONCAT('%',#{pName},'%')";
            if (!cName.isEmpty() && cName.length() > 0)
                sql += " and e.name_nvarchar like CONCAT('%',#{cName},'%')";
            if (DType != 0)
                sql += " and a.d_type_tinyint = #{DType}";
            if (ChkStatus != -1)
                sql += " and a.chk_status_tinyint = #{ChkStatus}";
            sql += " order by a.create_time_datetime desc" + " limit #{skip},#{limit})"
                    + " select a.rx_order_no_nvarchar as RxOrderNO,a.order_no_nvarchar as OrderNO,a.create_time_datetime as CreateTime,"
                    + " d.name_nvarchar as depName,a.open_doctor_nvarchar as OpenDoctor,e.name_nvarchar as Name,"
                    + " cast(a.d_type_tinyint as SIGNED INTEGER) as DType,cast(a.chk_status_tinyint as SIGNED INTEGER) as ChkStatus,"
                    + " a.chk_doctor_nvarchar as ChkDoctor,a.chk_time_datetime as ChkTime" + " from paget p"
                    + " inner join order_rx_main a on a.rx_order_no_nvarchar = p.rx_order_no_nvarchar"
                    + " inner join order_main b on a.order_no_nvarchar = b.order_no_nvarchar"
                    + " inner join doctor_base_info c on b.dc_id_bigint = c.dc_id"
                    + " inner join h_hospital_department d on c.depart_id = d.id_bigint"
                    + " inner join order_patients_info e on b.patients_info_id_bigint = e.id_bigint";
            return sql;
        }

        public String RxListCountSQL(@Param("rxorderno") String rxorderno, @Param("orderno") String orderno,
                @Param("dName") String dName, @Param("pName") String pName, @Param("cName") String cName,
                @Param("DType") byte DType, @Param("ChkStatus") int ChkStatus) {
            String sql = "select count(a.rx_order_no_nvarchar) from order_rx_main a"
                    + " inner join order_main b on a.order_no_nvarchar = b.order_no_nvarchar"
                    + " inner join order_patients_info e on b.patients_info_id_bigint = e.id_bigint" + " where 1 = 1";
            if (!rxorderno.isEmpty() && rxorderno.length() > 0)
                sql += " and a.rx_order_no_nvarchar like CONCAT('%',#{rxorderno},'%')";
            if (!orderno.isEmpty() && orderno.length() > 0)
                sql += " and a.order_no_nvarchar like CONCAT('%',#{orderno},'%')";
            if (!dName.isEmpty() && dName.length() > 0)
                sql += " and a.open_doctor_nvarchar like CONCAT('%',#{dName},'%')";
            if (!pName.isEmpty() && pName.length() > 0)
                sql += " and a.chk_doctor_nvarchar like CONCAT('%',#{pName},'%')";
            if (!cName.isEmpty() && cName.length() > 0)
                sql += " and e.name_nvarchar like CONCAT('%',#{cName},'%')";
            if (DType != 0)
                sql += " and a.d_type_tinyint = #{DType}";
            if (ChkStatus != -1)
                sql += " and a.chk_status_tinyint = #{ChkStatus}";
            return sql;
        }
    }
}
